package com.chungechunyemen.post.services.impl;

import java.util.List;

import com.chungechunyemen.post.dao.PostbarDAO;
import com.chungechunyemen.post.dao.RepliesDAO;
import com.chungechunyemen.post.dao.TopicDAO;
import com.chungechunyemen.post.po.Postbar;
import com.chungechunyemen.post.po.Replies;
import com.chungechunyemen.post.po.Topic;
import com.chungechunyemen.post.services.IPostService;

public class PostService implements IPostService {

	private TopicDAO topicDAO;
	private RepliesDAO repliesDAO;
	private PostbarDAO postbarDAO;

	public int postTopic(Topic temporaryVariable1, Replies temporaryVariable2) {
		int topicId = 0;
		// 持久化topic
		topicId = topicDAO.save(temporaryVariable1);
		// 设置reply的所属贴子ID,和title
		temporaryVariable2.setTopic(temporaryVariable1);
		temporaryVariable2.setTitle(temporaryVariable1.getSubject());
		// 持久化reply
		repliesDAO.save(temporaryVariable2);
		return topicId;
	}

	public int postReply(Replies temporaryVariable) {
		// 设置楼层数为最高楼数+1
		int maxPosition = repliesDAO.getMaxPostionByTopicId(temporaryVariable
				.getTopic().getTopicId());
		temporaryVariable.setPosition(maxPosition + 1);
		// 设置贴子回复数
		Topic topic = temporaryVariable.getTopic();
		topic.setReplies(temporaryVariable.getPosition());
		topicDAO.attachDirty(topic);
		repliesDAO.save(temporaryVariable);
		return 0;
	}

	public List viewTopicList(String temporaryVariable, int offset, int pageSize) {
		return topicDAO.findByPropertyAndPage("postbar.subject",
				temporaryVariable, offset, pageSize);
	}

	public int getTopicsCount(String temporaryVariable) {
		return topicDAO.findCountByProperty("postbar.subject",
				temporaryVariable);
	}

	public List viewRepliesList(int temporaryVariable, int offset, int pageSize) {
		return repliesDAO.findByPropertyAndPage("topic.topicId",
				temporaryVariable, offset, pageSize);
	}

	public void increaseTopicViews(int temporaryVariable) {
		Topic topic = topicDAO.findById(temporaryVariable);
		topic.setViews(topic.getViews() + 1);
		topicDAO.attachDirty(topic);
	}

	public void increaseTopicReplies(int temporaryVariable) {
		Topic topic = topicDAO.findById(temporaryVariable);
		topic.setReplies(topic.getReplies() + 1);
		topicDAO.getHibernateTemplate().update(topic);
	}

	public int getRepliesCount(int temporaryVariable) {
		return repliesDAO.findCountByProperty("topic.topicId",
				temporaryVariable);
	}

	public Postbar getPostbar(int temporaryVariable) {
		return (Postbar) postbarDAO.findById(temporaryVariable);
	}

	public Postbar getPostbar(String temporaryVariable) {
		List list = postbarDAO.findBySubject(temporaryVariable);
		if (list == null || list.size() == 0) {
			// 没有这个贴吧
			return null;
		} else {
			return (Postbar) list.get(0);
		}
	}

	public Topic getTopic(int temporaryVariable) {
		return topicDAO.findById(temporaryVariable);
	}

	// Setters
	public void setTopicDAO(TopicDAO topicDAO) {
		this.topicDAO = topicDAO;
	}

	public void setPostbarDAO(PostbarDAO postbarDAO) {
		this.postbarDAO = postbarDAO;
	}

	public void setRepliesDAO(RepliesDAO repliesDAO) {
		this.repliesDAO = repliesDAO;
	}
}
